﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraBars;
using DevExpress.XtraEditors.Controls;

using CRM.GUI.QLKhachHang;
using CRM.GUI.QLCuocGoi;
using CRM.GUI.HeThong;
using CRM.GUI.QLHeThong;


using BLL;
using DTO;
using System.Configuration;
using System.Threading;

namespace CRM.GUI
{
    public partial class frmMain : DevExpress.XtraEditors.XtraForm
    { 
        UserInfoEntity user = new UserInfoEntity();
        System.Configuration.Configuration config =
         ConfigurationManager.OpenExeConfiguration
                    (System.Windows.Forms.Application.ExecutablePath);

        public frmMain()
        {
            InitializeComponent();
            InitView();
            IntialViewforUser(user.UserID);
            CallManagement cm = new CallManagement();
            CallMonitor.GetNum = new CallMonitor.IncomingCallHandler(IncomingCallHandle);           
        }        

        void InitView()
        {
            var Pro = new CRM.GUI.Property();
            this.Height = Pro.HeightScreen;
            this.Width = Pro.WidthScreen;
            xtraTabbedMdiManager.MdiParent = this;
            
        }

        public Form mdiForm(Form frm)
        {
            foreach (Form f in MdiChildren)
            {
                if (f.Name == frm.Name)
                {
                    f.Activate();
                    return f;
                }
            }
            frm.MdiParent = this;
            frm.Show();
            return frm;
        }

        private void mbarExit_ItemClick(object sender, ItemClickEventArgs e)
        {
            DialogResult diaResult = MessageBox.Show(this, "Bạn Chắc Chắn Muốn Thoát Chương Trình???",
                                        "Xác Nhận Thoát Chương Trình", MessageBoxButtons.YesNo,
                                        MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            if (diaResult == DialogResult.Yes)
            {
                Environment.Exit(0);
            }            
        }

        private void mbtnDSCoban_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmBasicList f = new frmBasicList();
            mdiForm(f);
        }

        private void mbtnDSKHang_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmGeneralList f = new frmGeneralList();
            mdiForm(f);
        }

        private void mbtnDivide_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmDivide f = new frmDivide();
            f.ShowDialog();
        }

        private void mbtnDSKPI_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmKPIList f = new frmKPIList();
            mdiForm(f);
        }
        
        private void mbtnDSCuocGoi_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmCallDetail f = new frmCallDetail();
            mdiForm(f);            
        }

        private void mbarLogout_ItemClick(object sender, ItemClickEventArgs e)
        {
            DialogResult diaResult = MessageBox.Show(this, "Bạn Chắc Chắn Muốn Đăng Xuẫt???",
                                        "Xác Nhận Đăng Xuất", MessageBoxButtons.YesNo,
                                        MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            if(diaResult == DialogResult.Yes)
            {
                this.Hide();
                frmLogin f = new frmLogin();
                f.Show();
            }
        }
                

        private void mbarChangpass_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmChangePass f = new frmChangePass();
            f.ShowDialog();
        }

        private void mbtnTransfers_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmTransfer f = new frmTransfer();
            f.ShowDialog();
        }

        private void mbtnCTCuocGoi_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmManageCall f = new frmManageCall();
            mdiForm(f);
        }

        private void mbtnQLNDung_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmManageUser f = new frmManageUser();
            mdiForm(f);
        }        

        private void btnCheckCallServerConnect_ItemClick(object sender, ItemClickEventArgs e)
        {
            CallMonitor call = new CallMonitor();
            string IP = config.AppSettings.Settings["DomainName"].Value;
            string Account = config.AppSettings.Settings["Account"].Value;
            string Pass = config.AppSettings.Settings["Password"].Value;
            Int16 Port = Int16.Parse(config.AppSettings.Settings["Port"].Value);
            if (call.CheckAsteriskConnect(IP, Port, Account, Pass))
            {
                fbarCallServerState.Caption = "CallServer Ready";
            }
            else
            {
                fbarCallServerState.Caption = "Disconnected";
            }
        }

        private void btnCheckDatabaseConnect_ItemClick(object sender, ItemClickEventArgs e)
        {
            DatabaseFunc func = new DatabaseFunc();
            if (func.CheckState() != System.Data.ConnectionState.Open)
            {
                fbarDatabaseState.Caption = "Disconnected";
            }
            else
            {
                fbarDatabaseState.Caption = "Database Ready";
            }
        }

        private void mbtnInBound_ItemClick(object sender, ItemClickEventArgs e)
        {
            
        }

        private void mbarSetting_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmSystemConfig sys = new frmSystemConfig();
            sys.ShowDialog();
        }

        // Thiet lap view cho tung phan quyen
        private void IntialViewforUser(int userid)
        {
            Authentication au = new Authentication();            
            List<PermitDetailEntity> permit = au.LoadUserPermit(user.UserID);
            if (permit != null)
            {
                foreach (DevExpress.XtraBars.BarItem item in bar2.Manager.Items)
                {
                    foreach (PermitDetailEntity per in permit)
                    {
                        if (per.Status == true && per.PermissionName == item.Name)
                        {
                            item.Visibility = BarItemVisibility.Always;
                        }
                    }
                }
            }
            mbtnInBound.Visibility = BarItemVisibility.Never;
        }

        private void mbtnCatalog_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmManageCatalog f = new frmManageCatalog();
            mdiForm(f);
        }

        private void mbtnReport_ItemClick(object sender, ItemClickEventArgs e)
        {
            
        }
        private void IncomingCallHandle(string incomingnumber)
        {
            DataTranferEntity dt = new DataTranferEntity();
            if ( dt.IsBusy == false)
            {
                numbertmp = incomingnumber;

                IBDetailFunc _ibDetailFunc = new IBDetailFunc();
                if (numbertmp != null)
                {
                    if (_ibDetailFunc.LoadCustomerByPhone(numbertmp) == null)
                    {
                        IBDetailTempFunc _ibDetailFuncTemp = new IBDetailTempFunc();
                        string strName = "";

                        if (_ibDetailFuncTemp.LoadCustomerByPhone(numbertmp) != null)
                        {
                            strName = "Khách hàng : " + _ibDetailFuncTemp.LoadCustomerByPhone(numbertmp).LastName.ToString()
                                + " " + _ibDetailFuncTemp.LoadCustomerByPhone(numbertmp).FirstName.ToString() + "\n";
                        }

                        MessageBox.Show(strName + numbertmp.ToString() + " đang gọi ...", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        StartFormNewCall();

                    }
                    else
                    {
                        MessageBox.Show("Khách hàng : " + _ibDetailFunc.LoadCustomerByPhone(numbertmp).LastName.ToString()
                            + " " + _ibDetailFunc.LoadCustomerByPhone(numbertmp).FirstName.ToString() + "\n"
                            + numbertmp.ToString() + " đang gọi ...", "Thông Báo",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);

                        StartFormCall();

                    }

                    dt.IsBusy = false;
                }

            }
        }

        string numbertmp;
        frmNewCall f;
        private void StartFormNewCall()
        {
            f = new frmNewCall(numbertmp);
            if (f.InvokeRequired)
                f.Invoke(new MethodInvoker(StartFormNewCall));
            else
                f.ShowDialog();
                  
        }

        frmCall frm;
        private void StartFormCall()
        {
                frm = new frmCall(numbertmp);
                if (frm.InvokeRequired)
                    frm.Invoke(new MethodInvoker(StartFormCall));
                else
                    frm.ShowDialog();
        }
        
        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult diaResult = MessageBox.Show(this, "Bạn Chắc Chắn Muốn Thoát Chương Trình???",
                                           "Xác Nhận Thoát Chương Trình", MessageBoxButtons.YesNo,
                                           MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            if (diaResult == DialogResult.Yes)
            {
                Environment.Exit(0);
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void mbtnCADList_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmCADList frmCAD = new frmCADList();
            mdiForm(frmCAD);
        }
    }
}